package com.jzo2o.orders.manager.hander;

import cn.hutool.extra.spring.SpringUtil;
import com.jzo2o.orders.base.model.domain.Orders;
import com.jzo2o.orders.manager.model.dto.request.PlaceOrderReqDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

/**
 * @Author: lzj
 * 订单后续处理类
 *
 */
@Slf4j
public class AfterOrderHandler extends OrderHandler{

    private RabbitTemplate rabbitTemplate;

    public AfterOrderHandler() {
        this.rabbitTemplate = SpringUtil.getBean(RabbitTemplate.class);
    }


    @Override
    public void process(Orders orders, PlaceOrderReqDTO reqDTO) {
        log.info("订单后续处理");
        //orderHandler.process(orders, reqDTO);
        sendOrderCreateMessage(orders.getId());
    }

    /**
     * 发送订单创建延迟消息
     *
     * @param orderNo
     */
    private void sendOrderCreateMessage(Long orderNo) {
        log.info("发送订单延迟消息{}", orderNo);
        try {
            Message message = MessageBuilder.withBody(orderNo.toString().getBytes())
                    .setHeader("x-delay", 1000*60*15)
                    .build();
            rabbitTemplate.convertAndSend("jzo2o.delay.exchange", "cancel",message);
        } catch (Exception e) {
            log.error("发送订单延迟消息异常", e);
        }
    }
}
